#!/bin/bash

# 
# Pulling boot.bin from server.
# 
# Example usage:
# bootpull.sh ["./zedboard"]
# 
# Introduce:
# parameter is a path to store files that are pulled from server.
# 

version=V1.0
echo "The script version is $version."
server="http://192.168.50.4:8001/boot"

if ! [ -x "$(command -v wget)" ]; then
    echo 'Error: wget is not installed.' >&2
    exit 1
fi

function get_newest_ver()
{
    if [ ! -n $1 ];then
        return 1
    fi
    local url=$1

    local verfile=$(basename $0)
    verfile=${verfile%.*}
    wget -q "${url}" -O ".${verfile}_info.txt"
    local newest_ver=0
    while read line
    do
        if [[ $line =~ "<a href=\"" ]]
        then
            tmp=${line%%'/'*}
            tmp=${tmp#*\"}
            if [[ $tmp -gt $newest_ver ]];then
                newest_ver=$tmp
            fi
        fi
    done < ".${verfile}_info.txt"
    rm .${verfile}_info.txt
    echo "$newest_ver"
    return 
}

function download_boot()
{
    if [ ! -n $1 -o ! -n $2 -o ! -n $3 -o ! -n $4 ];then
        return 1
    fi

    local addr=$1
    local target=$2
    local ver=$3
    local label=$4
    local mode=${5:-"LVDS"}

    if [[ $label == "boot" ]];then
        wget -c "$addr/$mode/$ver/BOOT.bin" -O "$target/BOOT.bin"
    else 
        wget -c "$addr/$mode/$ver/BOOT.bin" -O "$target/BOOT_${mode}.bin"
    fi
    if [ ! -f $target/ver ];then
        touch "$target/ver"
    fi
    crudini --set "$target/ver" $label ver $ver
}

function update_boot_resever()
{
    if [ ! -n $1 -o ! -n $2 ];then
        return 1
    fi

    local url=$1
    local path=$2

    echo $path
    if [ ! -d $path ];then
        mkdir $path
    fi
    local mode="CMOS"
    local new=$(get_newest_ver "$url/$mode/")
    if [ -f "$path/ver" ];then
        local drvVer=$(crudini --get "$path/ver" $mode ver)
    else 
        local drvVer=0
    fi
    
    if [[ $new -gt $drvVer ]];then
        download_boot $url "$path" $new $mode $mode 
    fi
 
    mode="LVDS"
    new=$(get_newest_ver "$url/$mode/")
    if [ -f "$path/ver" ];then
        drvVer=$(crudini --get "$path/ver" $mode ver)
    fi
    
    if [[ $new -gt $drvVer ]];then
        download_boot $url "$path" $new $mode $mode 
    fi
}

path=${1:-"./zedboard"}
board=${path#*'/'}
mode=${2:-LVDS}
if [ ! -d $path ];then
    mkdir $path
fi

if [[ $board == "zedboard" ]] || [[ $board == "ZedBoard" ]];then
    board="ZedBoard"
elif [[ $board == "zc706" ]] || [[ $board == "ZC706" ]];then
    board="ZC706"
fi

update_boot_resever $server/$board "$path/boot"

if [ -f "$path/ver" ];then
    drvVer=$(crudini --get "$path/ver" boot ver)
    if [[ ! -n $drvVer ]];then
        drvVer=0
    fi
    mode=$(crudini --get "$path/ver" boot mode)
    if [[ ! -n $mode ]];then
        mode="LVDS"
        crudini --set "$path/ver" boot mode "LVDS"
    fi
else 
    touch "$path/ver"
    crudini --set "$path/ver" boot mode $mode
    crudini --set "$path/ver" boot ver 0
    drvVer=0
fi

new=$(get_newest_ver $server/$board/$mode/)
echo "new is $new, drvVer is $drvVer"
if [[ $new -gt $drvVer ]];then
    echo "Update boot files"
    download_boot "${server}/$board" $path $new "boot" $mode
    echo "Update boot files over."

    exit 0
fi

echo "Boot files is the newest, not need to update."
